<?php
use JPush\Client;
use JPush\JiGuang\JSMS;
class Jpush{
    public $app_key = 'b712ad86770b1084257061d2';
    public $master_secret = 'eb6887b22613835a76e69c28';


    public $www_app_key = 'db20236fa5660838212a125e';
    public $www_master_secret = '3b58674dfdf0521511b1dd6b';

    public $client;         //推送
    public $smsClient;      //短信
    public $registration_id = '';

    public $www_client;         //推送
    public $www_smsClient;      //短信
    public function __construct()
    {
        require_once "JPush/Client.php";
        require_once "JPush/JiGuang/JSMS.php";
        $this->client = new Client($this->app_key, $this->master_secret);       //极光推送
        $this->smsClient = new JSMS($this->app_key, $this->master_secret);       //极光短信

        $this->www_client = new Client($this->www_app_key, $this->www_master_secret);       //极光推送
        $this->www_smsClient = new JSMS($this->www_app_key, $this->www_master_secret);       //极光短信
    }

    /**
     * @func 极光推送
     * @param array $alias 别名
     * @param string $title 标题
     * @param string $detail 详情
     * @param array $route 路由
     * @return bool
     */
    public function pushmsg($alias=[],$title='',$detail='',$route=[])
    {
        $route = [
            'type'=> isset($route['type']) ? $route['type'] : 1,
            'path'=> isset($route['path']) ? $route['path'] : 'AwakeMsg',
            'title'=> isset($route['title']) ? $route['title'] : '我的消息',
            'url'=> isset($route['url']) ? $route['url'] : '',
            'data'=> isset($route['data']) ? $route['data'] : '',
            'notice'=> isset($route['notice']) ? $route['notice'] : null,
        ];

        $push = $this->client->push();
        $response = $push
            ->setPlatform(['ios','android'])
            ->addAlias($alias)        //别名推送
            //->addAllAudience()          //全推
            ->iosNotification($title, [
                'sound' => 'sound',
                'badge' => '+1',
                'extras' => [
                    'route'=>json_encode($route)
                ]
            ])
            ->androidNotification($detail,array(
                'title' => $title,
                'builder_id' => 2,
                'extras' => array(
                    'route'=>$route
                ),
            ))
            ->send();
        if(isset($response['http_code']) && $response['http_code'] == 200){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 极光短信
     */
    public function jgsms($phone,$temp_id=1){
        $result = $this->smsClient->sendCode($phone,$temp_id);
        if($result['http_code'] == 200){
            return ['code'=>true,'data'=>$result['body']['msg_id'],'msg'=>'短信发送成功'];
        }else{
            return ['code'=>false,'msg'=>$result['body']['error']['message']];
        }
    }


    /**
     * 验证短信
     */
    public function jgcheckcode($smg_id,$code){
        $result = $this->smsClient->checkCode($smg_id,$code);
        if($result['http_code'] == 200){
            return ['code'=>true,'msg'=>'验证码验证成功'];
        }elseif ($result['http_code'] == 404){
            return ['code'=>false,'msg'=>'请发送验证码'];
        }else{
            if($result['body']['error']['code']==50012){
                return ['code'=>false,'msg'=>'该验证码已经使用'];
            }elseif($result['body']['error']['code']==50011){
                return ['code'=>false,'msg'=>'验证码已经过期'];
            }elseif($result['body']['error']['code']==50010){
                return ['code'=>false,'msg'=>'验证码输入错误'];
            }else{
                return ['code'=>false,'msg'=>'由于网络原因，验证码没有验证通过'];
            }
        }
    }





    public function sendMessage($mobile,$temp_para = [])
    {
        $temp_id="184760";
        $result = $this->www_smsClient->sendMessage($mobile, $temp_id, $temp_para);
        if ($result['http_code'] == 200) {
            $f_verification_codes             = new \Common\Models\FVerificationCodes();
            $f_verification_codes->recipient  = $mobile;
            $f_verification_codes->code       = $temp_para['code'];
            $f_verification_codes->expire_at  = date('Y-m-d H:i:s', time() + 60 * 5);
            $f_verification_codes->created_at = date('Y-m-d H:i:s');
            $f_verification_codes->updated_at = date('Y-m-d H:i:s');
            $f_verification_codes->save();
            return ['code' => true, 'data' => $result['body']['msg_id'], 'msg' => '短信发送成功'];
        } else {
            return ['code' => false, 'msg' => $result['body']['error']['message']];
        }
    }


}
